/**
 * Project Name:jiuselu-parent
 * File Name:GoodsController.java
 * Package Name:com.fxp.project.jiuselu.web.business.v1.controller.goods
 * Date:2021-11-06 9:38 上午
 * Copyright (c) 2021, Jason.Wang All Rights Reserved.
 */
package com.fxp.project.jiuselu.web.business.v1.controller.goods;

import com.fxp.project.jiuselu.web.base.controller.BaseController;
import com.fxp.project.jiuselu.web.domain.CommonPage;
import com.fxp.project.jiuselu.web.domain.CommonResult;
import com.fxp.project.jiuselu.web.models.goods.entity.JslActivity;
import com.fxp.project.jiuselu.web.models.goods.request.ReqGoods;
import com.fxp.project.jiuselu.web.models.goods.service.IJslGoodsAttrService;
import com.fxp.project.jiuselu.web.models.goods.service.IJslGoodsService;
import com.fxp.project.jiuselu.web.models.goods.vo.GoodsAttrVo;
import com.fxp.project.jiuselu.web.models.goods.vo.GoodsListVo;
import com.fxp.project.jiuselu.web.models.goods.vo.GoodsVo;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.Collections;
import java.util.List;

/**
 * ClassName: GoodsController<br/>
 * Function: 商品管理的控制器<br/>
 * Date: 2021-11-06 9:38 上午<br/>
 *
 * @author Jason.Wang
 * @version 1.0.0
 * @see
 * @since JDK 8
 */
@RestController
@RequestMapping("/v1/goods")
public class GoodsController extends BaseController {

    IJslGoodsService jslGoodsService;

    IJslGoodsAttrService jslGoodsAttrService;

    @Autowired
    public void setJslGoodsService(IJslGoodsService jslGoodsService) {
        this.jslGoodsService = jslGoodsService;
    }

    @Autowired
    public void setJslGoodsAttrService(IJslGoodsAttrService jslGoodsAttrService) {
        this.jslGoodsAttrService = jslGoodsAttrService;
    }

    /**
     * list: 查询当前产品的列表信息<br/>
     *
     * @param reqGoods
     * @param result
     * @return
     * @author Jason.Wang
     * @date 2021/11/6 11:23 上午
     */
    @PostMapping("list")
    @CrossOrigin
    public CommonResult list(@RequestBody @Validated ReqGoods reqGoods, BindingResult result) {
        super.checkBindResult(result);
//        LocalDateTime currentDate =
//            super.setPageQueryTagWithOutLogin(Constants.INDEX_GOODS_LIST_CODE, reqGoods.getPage(), reqGoods.getCateId(),
//                reqGoods.getExtendCateId(), reqGoods.getThirdCateId(), reqGoods.getDiscover(), reqGoods.getSample(),
//                reqGoods.getKeyword());"null"
        String keyword =null;
//        if(reqGoods.getKeyword()!=null){
//            keyword = reqGoods.getKeyword().replace(" ", "");
//        }
        PageInfo<?> pageInfo = jslGoodsService.selectGoodsListPageInfo(reqGoods.getCategoryId(), reqGoods.getExtendCateId(),
            reqGoods.getThirdCateId(), reqGoods.getDiscover(), reqGoods.getSample(), reqGoods.getKeyword(),
            reqGoods.getPage(), reqGoods.getSize(), LocalDateTime.now(),reqGoods.getSalesCount(),reqGoods.getAttrPrice(),reqGoods.getGoodsSort());
        System.out.println(pageInfo.getTotal());
        //商品默认按照价格排序
//        List<GoodsListVo> list = (List<GoodsListVo>) pageInfo.getList();
//        list.sort((o1, o2) -> o1.getAttrPrice().compareTo(o2.getAttrPrice()));

        return CommonResult.success(CommonPage.restPage(pageInfo));
    }

    /**
     * detai: 获取当前商品的详细信息<br/>
     *
     * @param reqGoods
     * @param result
     * @return
     * @author Jason.Wang
     * @date 2021/11/7 10:28 上午
     */
    @PostMapping("detail")
    @CrossOrigin
    public CommonResult detail(@RequestBody @Validated ReqGoods reqGoods, BindingResult result) {
        super.checkBindResult(result);
        GoodsVo goodsVo = jslGoodsService.selectGoodsVoByGoodsId(reqGoods.getGoodsId());
        if (null != goodsVo) {
            List<GoodsAttrVo> goodsAttrVos = jslGoodsAttrService.selectGoodsAttrVoListByGoodsId(goodsVo.getGoodsId());
            goodsVo.setGoodsAttrList(goodsAttrVos);
            List<JslActivity> jslActivities = jslGoodsAttrService.selectGoodsActivityByGoodsId(goodsVo.getGoodsId());
            if(jslActivities.size() >0){
                LocalDateTime endTime = jslActivities.get(0).getActivityEndTime();
                long surplusMills=endTime.toEpochSecond(ZoneOffset.of("+8")) - LocalDateTime.now().toEpochSecond(ZoneOffset.of("+8"));
                if(jslActivities.size()>1){
                    Collections.reverse(jslActivities);
                }
                jslActivities.get(0).setSurplusMills(surplusMills);
            }
                goodsVo.setGoodsActivity(jslActivities);
        }
        return CommonResult.success(goodsVo);
    }
}
